<?php

namespace ticket\modules\backend\models;

use Yii;

/**
 * This is the model class for table "tk_ticket_type".
 *
 * @property int $id
 * @property int $app_id
 * @property string $type_name
 * @property string $descript
 * @property string $state
 * @property string $create_time
 */
class TicketType extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tk_ticket_type';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['type_name', 'descript'], 'required'],
            [['app_id'], 'integer'],
            [['state','image_url'], 'string'],
            [['create_time'], 'safe'],
            [['type_name'], 'string', 'max' => 32],
            [['descript'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'app_id' => 'App Id',
            'type_name' => 'Type Name',
            'descript' => 'Descript',
            'image_url' => 'Image Url',
            'state' => 'State',
            'create_time' => 'Create Time',
        ];
    }

    /*** 添加类别
     * @param $aid
     * @param $params
     * @return bool
     */
    public function addType($aid,$params)
    {
        $data = $this->setData($params);
        $data['TicketType']['app_id'] = $aid;
        $data['TicketType']['create_time'] = date("Y-m-d H:i:s");

        if ($this->load($data) and $this->validate()){
            if ($this->save(0)){
                return true;
            }
        }
        return false;
    }

    /*** 修改分类
     * @param $id
     * @param $params
     * @return bool
     */
    public function modType($id,$params)
    {
        $data = $this->setData($params);
        if ($this->load($data) and $this->validate()){
            $model = self::find()->where('id = :id',[':id' => $id])->one();
            if (!is_null($model)){
                $model->type_name = $data['TicketType']['type_name'];
                $model->descript = $data['TicketType']['descript'];
                $model->image_url = $data['TicketType']['image_url'];
                $model->state = $data['TicketType']['state'];
                if ($model->save(0)){
                    return true;
                }
            }
        }
        return false;
    }

    /*** 获取单个分类信息
     * @param $id
     * @return array|null|\yii\db\ActiveRecord
     */
    public function getTypeInfo($id)
    {
        $data = self::find()->where('id = :id',[':id' => $id])->asArray()->one();
        return $data;
    }


    /*** 删除分类
     * @param $id
     * @return bool
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public function delTicketType($id)
    {
        $model = self::find()->where('id = :id',[':id' => $id])->one();

        if (!is_null($model)){
            if ($model->delete()){
                return true;
            }
        }
        return false;


    }

    public function getTypeList($aid)
    {
        $data = self::find()->where('app_id = :id',[':id' => $aid])->select(['id','type_name'])->asArray()->all();
        return $data;
    }



    private function setData($params)
    {

        if (isset($params['type_name']) and !empty($params['type_name'])){
            $data['TicketType']['type_name'] = $params['type_name'];
        }else{
            $data['TicketType']['type_name'] = '';
        }
        if (isset($params['descript']) and !empty($params['descript'])){
            $data['TicketType']['descript'] = $params['descript'];
        }else{
            $data['TicketType']['descript'] = '';
        }
        if (isset($params['state']) and !empty($params['state'])){
            $data['TicketType']['state'] = $params['state'];
        }else{
            $data['TicketType']['state'] = '1';
        }
        if (isset($params['image_url']) and !empty($params['image_url'])){
            $data['TicketType']['image_url'] = json_encode($params['image_url']);
        }else{
            $data['TicketType']['image_url'] = '1';
        }
        return $data;
    }
}
